﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Domain.Commands;

namespace Agr.Questionnaire.Core.Domain.Qnnaire.Commands
{
    public class SetTextAnswerTemplateOnAnswerableQuestion : Command
    {
        internal string SectionName { get; set; }
        internal int QuestionSequenceNumber { get; set; }
    }

    public class SetTextAnswerTemplateOnAnswerableQuestionHandler : CommandHandler<SetTextAnswerTemplateOnAnswerableQuestion>
    {
        protected override void ExecuteInternal(SetTextAnswerTemplateOnAnswerableQuestion command)
        {
            var qnnaire = GetById<Questionnaire>(command.AffectedAggregateRootId, command.AffectedVersion);

            var s = qnnaire.GetSection(command.SectionName);
            if (s == null)
                throw new InvalidCommandException("Section {0} was not found", command.SectionName);

            var q = s.GetQuestion(command.QuestionSequenceNumber);
            if (q == null)
                throw new InvalidCommandException("Question {0} in section {1} was not found", command.QuestionSequenceNumber, command.SectionName);

            q.UseTextAnswerTemplate();
        }
    }

}
